import java.awt.Point;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class PuzzleGenerator {
	private int numRows = 0, numCols = 0;
	private int index = 0;
	private int manyInserted = 0;
	private PuzzleBoard theBoard = new PuzzleBoard(10, 10);
	private WordList wordList = new WordList();
	private final Point offBoard = new Point(-1, -1);
	private boolean needGoodPoint = true;
	public PuzzleGenerator(PuzzleBoard board, WordList list) {
		theBoard = board;
		wordList = list;
		numRows = board.getWidth();
		numCols = board.getHeight();
	}

	public void createPuzzle() {
		for (int i = 0; i < wordList.getWordCount(); i++) {
			System.out.println("Adding Word: " + wordList.getWord(i) + "\n");
			generatePuzzle(wordList.getWord(i));
			index++;
			needGoodPoint = true;
		}
		System.out.println("Generation Complete \n");

	}

	public void generatePuzzle(String addWord) {
		int[] tmpPlacement = new int [3];
		int[] placement = new int [3];
		
		while((needGoodPoint)){
			System.out.println("LOOKING FOR GOOD POINT STILL");
			tmpPlacement = findPositionfor(addWord);
		}
		placement = tmpPlacement;
		Point currPoint = new Point();
		currPoint.x = placement[0];
		currPoint.y = placement[1];
		System.out.println("Inserting Word: " + addWord + " --in Direction: "
				+ placement[2] + "-->at Position: " + currPoint.x + ","
				+ currPoint.y + "\n");
		switch (placement[2]) {
		//try {
			//switch (2) {
			case 1:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					System.out.println("Inside Generator...inserting: "
							+ wordList.getWord(index).charAt(i) + "at Point: "
							+ currPoint.x + currPoint.y);
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					// theBoard.setChar(currPoint.x, currPoint.y, 'P');
					currPoint = theBoard.getUpLeft(currPoint.x, currPoint.y);
					System.out.println("Inside Generator...after insert: "
							+ wordList.getWord(index).charAt(i) + "new Point: "
							+ currPoint.x + currPoint.y);
				}
				manyInserted++;
				break;
			case 2:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					// theBoard.setChar(currPoint.x, currPoint.y, 'P');
					currPoint = theBoard.getUp(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 3:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getUpRight(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 4:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getLeft(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 5:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getRight(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 6:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getDownLeft(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 7:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getDown(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;
			case 8:
				for (int i = 0; i < wordList.getWord(index).length(); i++) {
					theBoard.setChar(currPoint.x, currPoint.y, wordList
							.getWord(index).charAt(i));
					currPoint = theBoard.getDownRight(currPoint.x, currPoint.y);
				}
				manyInserted++;
				break;

			}
			/*} catch (Exception e) {
			findPositionfor(addWord);
		}*/

	}

	/*
	 * private int[] findPositionfor(String word) { int[] pos = new int[3];
	 * Point rPoint = new Point(); Point nPoint = new Point(); rPoint =
	 * getRandomPosition(); nPoint = rPoint; int direction =
	 * getRandomDirection(); switch (direction) { case 1: for (int i = 0; i <
	 * word.length(); i++) { if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0)
	 * { if (word.charAt(i) == theBoard.getChar(nPoint.x, nPoint.y)) {
	 * 
	 * } else { findPositionfor(word); } } nPoint = theBoard.getUpLeft(nPoint.x,
	 * nPoint.y); } break; } return pos; }
	 */
	private int[] findPositionfor(String word) {
		
		Point rPoint = new Point();
		Point nPoint = new Point();
		rPoint = getRandomPosition();
		nPoint = rPoint;
		int direction = getRandomDirection();
		int x = 0;
		int y = 0;
		try {
			 switch (direction) {
			//switch (2) {
			case 1:
				for (int i = 0; i < word.length(); i++) {
					x = nPoint.x;
					y = nPoint.y;
					System.out.println("Checking point:" + x + ","+ y);
					if (validPoint(nPoint)) {						
						break;
					} 
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					}else if ((theBoard.getWordCount(nPoint.x, nPoint.y) > 0)) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getUpLeft(nPoint.x, nPoint.y);
				}
				break;
			case 2:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;

					}
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					} else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;

						}
					}
					nPoint = theBoard.getUp(nPoint.x, nPoint.y);
				}
				break;
			case 3:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					}
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					} else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getUpRight(nPoint.x, nPoint.y);
				}
				break;
			case 4:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					}
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					} else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getLeft(nPoint.x, nPoint.y);
				}
				break;
			case 5:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					} 
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					}else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getRight(nPoint.x, nPoint.y);
				}
				break;
			case 6:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					}
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					} else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getDownLeft(nPoint.x, nPoint.y);
				}
				break;
			case 7:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					}
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					} else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getDown(nPoint.x, nPoint.y);
				}
				break;
			case 8:
				for (int i = 0; i < word.length(); i++) {
					if (validPoint(nPoint)) {
						break;
					} 
					if(i == word.length() -1)
					{
						needGoodPoint = false;
						break;
					}else if (theBoard.getWordCount(nPoint.x, nPoint.y) > 0) {
						if (word.charAt(i) == theBoard.getChar(nPoint.x,
								nPoint.y)) {

						} else {
							break;
						}
					}
					nPoint = theBoard.getDownRight(nPoint.x, nPoint.y);
				}
				break;
			}
		} catch (Exception e) {
			
		}
		int[] pointDir = new int[3];
		pointDir[0] = rPoint.x;
		pointDir[1] = rPoint.y;
		pointDir[2] = direction;
		return pointDir;
	}

	public boolean validPoint(Point p) {
		// int i = p.equals(offBoard) ? 0 : 1;
		// System.out.println("_____________________________________________________________Validating Point"
		// + i);
		// return p.equals(offBoard) ? true : false;
		if (p.x == -1 || p.y == -1)
			return true;
		if (p.x >= numRows || p.y >= numCols)
			return true;
		if (p.x < 0 || p.y < 0)
			return true;
		else
			return false;

	}

	public Point getRandomPosition() {
		Random generator = new Random();
		Point randomPoint = new Point();
		// took out the plus 1....arrays start at 0...
		// randomPoint.x = generator.nextInt(numCols) + 1;
		// randomPoint.y = generator.nextInt(numRows) + 1;
		randomPoint.x = generator.nextInt(numCols);
		randomPoint.y = generator.nextInt(numRows);
		//randomPoint.x = 3;
		//randomPoint.y = 9;
		return randomPoint;
	}

	public int getRandomDirection() {
		Random generator = new Random();
		int randomNumber = generator.nextInt(8) + 1;
		return randomNumber;
	}

}
